package com.rgs.ssm.dao;

import com.rgs.ssm.domain.Member;
import com.rgs.ssm.domain.Orders;
import com.rgs.ssm.domain.Product;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @create 2021-02-17 14:35
 **/

@Repository
public interface IOrderDao {

    /**
     * 添加订单
     * @param orders
     */
    @Insert("insert into orders(orderNum,orderTime,peopleCount,payType,orderStatus,productId,memberId)" +
            "values(#{orderNum},#{orderTime},#{peopleCount},#{payType},#{orderStatus},#{productId},#{memberId})")
    void saveOrders(Orders orders);

    /**
     * 删除
     * @param id
     */
    @Delete("delete from orders where id = #{id}")
    void delete(Integer id);

    /**
     * 修改
     * @param orders
     */
    @Update("update orders set orderNum=#{orderNum},orderTime=#{orderTime},peopleCount=#{peopleCount},payType=#{payType},orderStatus=#{orderStatus},productId=#{productId},memberId=#{memberId} where id = #{id}")
    void update(Orders orders);

    @Select("select * from orders")
    @Results({
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "orderNum",column = "orderNum"),
            @Result(property = "orderTime",column = "orderTime"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "peopleCount",column = "peopleCount"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "orderDesc",column = "orderDesc"),
            @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select="com.rgs.ssm.dao.IProductDao.findById"))


    })
    public List<Orders> findAll();

    @Select("select * from orders where id = #{id}")
    @Results({
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "orderNum",column = "orderNum"),
            @Result(property = "orderTime",column = "orderTime"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "peopleCount",column = "peopleCount"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "orderDesc",column = "orderDesc"),
            @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select="com.rgs.ssm.dao.IProductDao.findById")),
            @Result(property = "member",column = "memberId",javaType = Member.class,one = @One(select="com.rgs.ssm.dao.IMemberDao.findById")),
            @Result(property = "travellers",column = "id",javaType = java.util.List.class,many = @Many(select="com.rgs.ssm.dao.ITravellerDao.findByOrderId"))
    })
    public Orders findById(Integer id);
}
